<?php
/**
 * Created by PhpStorm.
 * User: dev
 * Date: 23/9/14
 * Time: 12:56 PM
 */

class Admin_Settings_Email_Controller extends Admin_Controller {

    public $restful = true;

    public function __construct()
    {
        parent::__construct();
        //--Your Code --//
        $this->theme->set_theme("admin") ;
        $this->theme->set_layout("default") ;

    }

    public function get_index(){
    	if (Auth::guest()) return Redirect::to('admin/login');
    	 
    	if (!Auth::check())
    	{
    		return Redirect::to('admin/login');
    	}else{
        $data['lang'] = $this->lang;
        $data['title'] = Settings::get($this->lang, 'site_title');
        $this->theme->title("Email Settings Settings");
        return $this->theme->render('admin::settings.email',$data);
    	}
    }

    public function post_index(){
        if(Input::get('btnSave') == 'save'){
        	
        	$rules = array(
        			'host' => 'required',
        			'port' => 'required',
        			'userName' => 'required',
        			'userPassword' => 'required',
        			'footer' => 'required',
                    'default_email' => 'required',
        			);
        	
        	$validator = Validator::make(Input::get(), $rules);
        	if( $validator->fails() )
        	{
        		return Redirect::back()->with_input()->with_errors($validator)->with('message','<div class="alert alert-danger">Invalid Operation !</div>');
        	
        	}else{

            Settings::saveSetting('mail_smtp', trim(Input::get('opMail')));
            Settings::saveSetting('mail_smtp_auth', trim(Input::get('opAuth')));
            Settings::saveSetting('mail_smtp_host', trim(Input::get('host')));
            Settings::saveSetting('mail_smtp_ssl', trim(Input::get('opSSL')));
            Settings::saveSetting('mail_smtp_port', trim(Input::get('port')));
            Settings::saveSetting('mail_smtp_user', trim(Input::get('userName')));
            Settings::saveSetting('mail_smtp_password', trim(Input::get('userPassword')));
            Settings::saveSetting('mail_smtp_footer', trim(Input::get('footer')));
            Settings::saveSetting('default_email', trim(Input::get('default_email')));
        	}
        }

        if(Input::get('btnTest') == 'mail'){

            $p = array(
                'from' => trim(Settings::get('en', 'default_email')),
                'to' => trim(Settings::get('en', 'default_email')),
                'sender_name' => 'Test Email',
                'reply_to' => 'noreply@gmail.com',
                'subject' => 'Testing Email',
                'body' => 'Ah! This very nice.'
            );

            if(Commoncall::mail($p)){
                return Redirect::back()->with('message', 'Test Mail sent..');
            }else{
                return Redirect::back()->with('message', 'Test Mail Fail..');
            }
        }
        return Redirect::back()->with('message', '<div class="alert alert-info">Done..</div>');
    }


}
